
let imgObs = new IntersectionObserver(function(entries){
    // 循环进入视窗的元素
    entries.forEach((ent)=>{
        // 判断元素是否处于视窗中
        if(!ent.isIntersecting) return;
        // 对原图片进行样式操作
        let fit = ent.target.style.objectFit;
        let pos = ent.target.style.objectPosition;
        if(ent.target.src==""){
            ent.target.src="";
            ent.target.style.objectFit="scale-down";
            ent.target.style.objectPosition="center";
        }
        // 图片预加载
        let imgPath = ent.target.imgPath;
        let img = new Image();
        img.src = imgPath;
        img.onload = function(){
            ent.target.style.objectFit=fit;
            ent.target.style.objectPosition=pos;
            ent.target.src = imgPath;
        }
    })
})

export default function(app){
    app.directive("lazy",function(el,binding){
        el.imgPath = binding.value;
        imgObs.observe(el);
    })
}

